package userController

import (
	"fmt"
	"git.oschina.net/zd04/beego-shop/controllers"
	"git.oschina.net/zd04/beego-shop/models/userModel"
	"github.com/astaxie/beego"
)

//login
type LoginUserController struct {
	controllers.MainController
}

func (this *LoginUserController) Get() {
	if this.IsLogin {
		this.Redirect("/", 302)
	}
	this.TplName = "user/login.tpl"
}

func (this *LoginUserController) Post() {
	username := this.GetString("username")
	password := this.GetString("password")

	if "" == username {
		this.Data["json"] = map[string]interface{}{"code": 0, "message": "请填写用户名"}
		this.ServeJSON()
	}

	if "" == password {
		this.Data["json"] = map[string]interface{}{"code": 0, "message": "请填写密码"}
		this.ServeJSON()
	}
	users, err := userModel.LoginUser(username, password)

	if err == nil {
		this.SetSession("userLogin", fmt.Sprintf("%d", users.Id)+"||"+users.Username)

		this.Data["json"] = map[string]interface{}{"code": 1, "message": "贺喜你，登录成功"}
	} else {
		this.Data["json"] = map[string]interface{}{"code": 0, "message": "登录失败!" + err.Error()}
	}
	this.ServeJSON()
}

//logout
type LogoutUserController struct {
	controllers.BaseController
}

func (this *LogoutUserController) Get() {
	this.DelSession("userLogin")
	this.Redirect("/login", 302)
}

type ListUserController struct {
	controllers.MainController
}

func (this *ListUserController) Get() {

	nums, err, userlists := userModel.ListUserByCond()
	if err != nil {
		beego.Debug(err)
	} else if nums == 0 {
		beego.Debug(nums)
	}
	this.Data["json"] = userlists
	this.ServeJSON()
	// _, userlist1, _ := userModel.ListUser()
	// this.Data["json"] = userlist1
	// this.ServeJSON()

	// userlist, _ := userModel.Management()
	// this.Data["users"] = userlist

	// this.Layout = "layout/layout.tpl"
	// this.TplName = "user/users.tpl"
}

type AddUserController struct {
	controllers.MainController
}

func (this *AddUserController) Get() {
	this.TplName = "user/add.tpl"
}

func (this *AddUserController) Post() {
	username := this.GetString("username")
	password := this.GetString("password")

	if "" == username {
		this.Data["json"] = map[string]interface{}{"code": 0, "message": "请填写用户名"}
		this.ServeJSON()
	}

	if "" == password {
		this.Data["json"] = map[string]interface{}{"code": 0, "message": "请填写密码"}
		this.ServeJSON()
	}

	user, err := userModel.GetUserByName(username)
	if err != nil {
		this.Data["json"] = map[string]interface{}{"code": 0, "message": "添加失败!" + err.Error()}
	} else if user.Id != 0 {
		this.Data["json"] = map[string]interface{}{"code": 0, "message": "添加失败!账号已经存在了!"}
	} else {
		_, err = userModel.AddUser(username, password)

		if err == nil {
			this.Data["json"] = map[string]interface{}{"code": 1, "message": "贺喜你，添加成功"}
		} else {
			this.Data["json"] = map[string]interface{}{"code": 0, "message": "添加失败"}
		}
	}

	this.ServeJSON()
}

type UpdateUserController struct {
	controllers.MainController
}

func (this *UpdateUserController) Get() {
	userid, _ := this.GetInt64("id")
	user, err := userModel.GetUser2(userid)

	if err == nil {
		this.Data["user"] = user
		this.Data["json"] = user
		this.ServeJSON()
	}
	this.TplName = "user/update.tpl"
}

func (this *UpdateUserController) Post() {
	userid, _ := this.GetInt64("id")
	username := this.GetString("username")
	password := this.GetString("password")

	if "" == username {
		this.Data["json"] = map[string]interface{}{"code": 0, "message": "请填写用户名"}
		this.ServeJSON()
	}

	if "" == password {
		this.Data["json"] = map[string]interface{}{"code": 0, "message": "请填写密码"}
		this.ServeJSON()
	}
	_, err := userModel.UpdateUser3(userid, username, password)

	if err == nil {
		this.Data["json"] = map[string]interface{}{"code": 1, "message": "贺喜你，更新成功"}
	} else {
		this.Data["json"] = map[string]interface{}{"code": 0, "message": "更新失败"}
	}
	this.ServeJSON()
}
